* Creates connected sets used in analysis

clear all

use "$savedata/ami_analysis_mainspef.dta"

* Drop very young patients 
drop if derv_age<35

* Only want to look at hospitals that treat a reasonable amount of patients
*egen hosp_vol = sum(ami), by(trust_code)
egen hosp_vol_year = sum(ami), by(trust_code finyear)

* Restrict to hospitals that treat at least 100 heart attack patients in a year to be included in the data
keep if hosp_vol_year>99


* Now I want to put restrictions on the minimum number of patients treated by docs in each hospital - we want to compare people doing reasonable volumes
egen vol = sum(ami), by(pconsult)
egen vol_trust = sum(ami), by(pconsult trust_code)

drop if vol_trust<10

* Also check when doing this by year
egen vol_year = sum(ami), by(pconsult finyear)

egen vol_trust_year = sum(ami), by(pconsult trust_code finyear)


* Restrict on the basis of minimum consultant-trust pair volumes
foreach x in 10 25 50 100{
	
preserve
* Only want to be linked by docs who treat x or more patients in a given hospital
collapse (sum) ami, by(trust_code pconsult)
keep if ami>=`x'

egen constotal = sum(ami), by(pconsult)
rename ami trust_constotal

** Connection being that doctors work in at least 2 hospitals; with a minimum volume in total

* Number of hospitals worked in a given year
bys pconsult: gen x=_n
egen total_trusts_worked_in = max(x), by(pconsult)

* Keep only the connectors (those in more than one hospital in a given year)
keep if total_trusts>1

* Now keep only those who do a minimum of 10 or more
keep if trust_constotal>`x'

* for graphs, want to plot these
egen mincheck = min(trust_constotal), by(pconsult)
egen maxcheck = max(trust_constotal), by(pconsult)

gen akm_set_mainspef`x' = 1
gen akm_connectors`x' = 1
egen akm_connecting_pats`x' = sum(trust_constotal), by(trust_code)

collapse (max) akm_set_mainspef`x' (sum) akm_connectors`x' akm_connecting_pats`x', by(trust_code)

save "$savedata/connect_total`x'.dta", replace
count

restore
}

* Using trust-year values

foreach x in 10 20 25 50{
	
preserve
* Only want to be linked by docs who treat x or more patients in a given hospital-year
collapse (sum) ami, by(trust_code pconsult finyear)

* This is new as of 14th July 2024
keep if ami>=`x'

egen constotal = sum(ami), by(pconsult finyear)
rename ami trust_constotal

* Number of hospitals worked in a given year
bys pconsult finyear: gen x=_n
egen total_trusts_worked_in = max(x), by(pconsult finyear)

* Keep only the connectors (those in more than one hospital in a given year)
keep if total_trusts>1

* Now keep only those who do a minimum of 10 or more
keep if trust_constotal>`x'

* for graphs, want to plot these
egen mincheck = min(trust_constotal), by(pconsult finyear)
egen maxcheck = max(trust_constotal), by(pconsult finyear)

* This is the original connected set

gen tv_set_mainspef`x' = 1
gen tv_connectors`x' = 1
egen tv_connecting_pats`x' = sum(trust_constotal), by(trust_code finyear)

collapse (max) tv_set_mainspef`x' (sum) tv_connectors`x' tv_connecting_pats`x', by(trust_code finyear)

save "$savedata/connect_year`x'.dta", replace
count
unique trust_code

restore
}

